<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\material;

use app\model\mongo\MongoVideoModel;
use app\model\mysql\ZcVideoPeopleModel;
use app\model\VideoModel;
use app\model\ZcStoreModel;
use app\model\ZcTaskModel;
use app\service\BaseService;
use app\service\company\DepartmentService;
use app\service\creative\StoreService;
use app\service\creative\TaskService;
use app\service\material\base\StaffTrait;
use app\service\material\base\TaskTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

final class VideoService extends BaseService
{
    use TaskTrait;
    use StaffTrait;

    /**
     * 获取视频相关人员.
     *
     * @param VideoModel $model 视频模型
     * @return array|bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getVideoStaff(VideoModel $model)
    {
        if (! $model->getKey()) {
            return [];
        }
        // 非系统任务视频
        if (! self::isTask($model)) {
            return [];
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }

        $storeModel = StoreService::getStoreById($taskModel->getAttr('store_id'));
        if (! $storeModel instanceof ZcStoreModel || ! $storeModel->getKey()) {
            return self::setError('未知店铺');
        }

        $subtaskId = intval($model->getAttr('subtask_id'));
        $teamId = intval($taskModel->getAttr('team_id'));
        $departmentId = intval($storeModel->getAttr('department_id'));

        // 获取创意导演
        $staffs['dy_id'] = self::getCreativeDirectStaff($subtaskId) ?: 0;
        // 获取创意参与人
        $creativeStaffId = self::getCreativeStaff($subtaskId);
        if ($staffs['dy_id']) {
            $creativeStaffId[] = $staffs['dy_id'];
        }
        $creativeStaffId = array_unique($creativeStaffId);
        $staffs['all_staff_id'] = $creativeStaffId;
        $staffs['creative_staff_id'] = $creativeStaffId;
        // 获取创意组负责人
        $staffs['creative_team_leader_id'] = self::getCreativeTeamStaff($teamId) ?: 0;
        if ($staffs['creative_team_leader_id']) {
            $staffs['all_staff_id'][] = $staffs['creative_team_leader_id'];
        }
        // 获取创意部门负责人
        $staffs['creative_department_leader_id'] = self::getCreativeDeptStaff($departmentId) ?: 0;
        if ($staffs['creative_department_leader_id']) {
            $staffs['all_staff_id'][] = $staffs['creative_department_leader_id'];
        }
        // 获取创意制片
        $staffs['zp_id'] = self::getCreativeProductionStaff($teamId, $departmentId) ?: 0;
        if ($staffs['zp_id']) {
            $staffs['all_staff_id'][] = $staffs['zp_id'];
        }
        // 获取创意设计
        $staffs['sj_id'] = 0;
        // 获取创意后期
        $staffs['hq_id'] = 0;
        // 获取创意编导
        $staffs['bd_id'] = 0;
        // 获取创意摄像
        $staffs['ps_id'] = 0;
        $creativeRole = self::getCreativeRoleStaff($subtaskId);
        if ($creativeRole) {
            foreach ($creativeRole as $item) {
                if ($item['role_id'] == 15) {
                    $staffs['bd_id'] = $item['user_id'];
                }
                if ($item['role_id'] == 3) {
                    $staffs['ps_id'] = $item['user_id'];
                }
                if ($item['role_id'] == 4) {
                    $staffs['hq_id'] = $item['user_id'];
                }
                if ($item['role_id'] == 5) {
                    $staffs['sj_id'] = $item['user_id'];
                }
            }
        }
        // 运营人员
        $staffs['operation_id'] = self::getVideoTaskOperation($model);
        if ($staffs['operation_id']) {
            $staffs['all_staff_id'][] = $staffs['operation_id'];
        }
        $staffs['all_staff_id'] = array_unique($staffs['all_staff_id']);
        return $staffs;
    }

    /**
     * 获取视频任务运营发起人.
     *
     * @param VideoModel $model 视频模型
     * @return array|bool|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getVideoTaskOperation(VideoModel $model)
    {
        if (! $model->getKey()) {
            return [];
        }
        // 非系统任务视频
        if (! self::isTask($model)) {
            return [];
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }
        return intval($taskModel->getAttr('create_uid'));
    }

    /**
     * 获取视频任务创意团队.
     *
     * @param VideoModel $model 视频模型
     * @return array|bool|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getVideoTaskTeam(VideoModel $model)
    {
        if (! $model->getKey()) {
            return [];
        }
        // 非系统任务视频
        if (! self::isTask($model)) {
            return [];
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }
        return intval($taskModel->getAttr('team_id'));
    }

    /**
     * 获取视频演員.
     *
     * @param VideoModel $model 视频模型
     */
    public static function getVideoPerformer(VideoModel $model): array
    {
        if (! $model->getKey()) {
            return [];
        }
        return (new ZcVideoPeopleModel())->where('video_id', $model->getAttr('file_id'))->column('people_id');
    }

    /**
     * 获取视频所属区域
     *
     * @param VideoModel $model 视频模型
     * @return bool|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getVideoDepartmentArea(VideoModel $model)
    {
        if (! $model->getKey()) {
            return 0;
        }
        // 非系统任务视频
        if (! self::isTask($model)) {
            return 0;
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }

        $storeModel = StoreService::getStoreById($taskModel->getAttr('store_id'));
        if (! $storeModel instanceof ZcStoreModel || ! $storeModel->getKey()) {
            return self::setError('未知店铺');
        }
        $departmentId = intval($storeModel->getAttr('department_id'));
        return DepartmentService::getAreaByCache($departmentId);
    }

    /**
     * 修改视频使用状态.
     * @param string $signature md5
     */
    public static function videoUsed(string $signature = ''): bool
    {
        $model = (new MongoVideoModel())->where('signature', $signature)->findOrEmpty();
        if (! $model->getKey() || $model->getAttr('is_use') == $model::USE_ENABLE) {
            return true;
        }
        $model->setAttr('is_use', $model::USE_ENABLE);
        return $model->save();
    }
}
